
import ProxyLists from "proxy-lists";
import { ExtractClientInterface, ProxyInterface } from "../etl";
import { getLogger } from "log4js";

const logger = getLogger(`proxy-lists`)
export class ProxyListsImpl implements ExtractClientInterface {
	async * extract(): AsyncGenerator<ProxyInterface, void, any> {
		let proxies: any[] = []

		try {
			proxies = await this.getProxy()
		} catch (error) {
			logger.error(`ProxyListsImpl extract promise getProxy method ${error}`)
		}
		for (const item of proxies) {
			yield item
		}

	}

	getProxy(): Promise<any[]> {
		let proxyArr: any[] = []
		return new Promise((resolve, reject) => {
			ProxyLists.getProxies({})
				.on('data', async (proxies) => {
					proxyArr = proxyArr.concat(proxies)
				})
				.on('error', function (error: Error) {
					logger.error(error.message)
					reject(error.message)
				})
				.once('end', function () {
					resolve(proxyArr)
				});
		})
	}
}
